class InterestsController < ApplicationController
	
	  layout 'default'
  
	# adds an interest of the user from a given tag name
	def addRelation
		if params[:commit] then
	   	@user = User.find(params[:id])
		@tags = (params[:theTag][:tagName]).split
		@tags.each {|tag|   
		@tag = Tag.find(:first , :conditions => "name = '#{tag}'")
		if(@tag.nil?) 
			@tag = createTagWithName tag
	   	end
		@interest = Interest.find(:first , :conditions => "tag_id=#{@tag.id} AND  user_id = #{@user.id}")
		if(@interest.nil?) then
		createInterestWithTagandUser(@tag, @user)
		else 
			flash[:notice] = "tag #{@tag.name} already present for user #{@user.name}"
		end
		}
		redirect_to :controller => "users" , :action => "start_page"
		
	end
end

	
# helper
  def createTagWithName(tag_name)
	  tag = Tag.new
	  tag.name = tag_name
	  tag.counter = 0
	  tag.save
	  return tag
  end
  
#  helper
  def createInterestWithTagandUser(tag,user)
	  interest = Interest.new
	  interest.user_id = user.id
	  interest.tag_id = tag.id
	  interest.save
  end	
  
  def addTagToProfile
  	@controller2 = "interests"
	@object = User.find(params[:id])
  	render :partial => "/tags/editspecial"
  end   
	
  def destroy
    @interest = Interest.find(params[:id])
    if(session[:user_id] == @interest.user_id) then
    @interest.destroy
    end
	redirect_to :action => "show", :controller => "users" ,:id => @interest.user_id
  end	
  
  def show
  
    @tag = Tag.find(params[:tag_id])
    @interest = Interest.find(:first , :conditions => "tag_id = #{params[:tag_id]} AND user_id = #{params[:user_id]}")
    flash[:notice] = @interest.user_id
	respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @tag }
    end
  end  
	
end
